package com.edu.service.impl;
/*
 * @Author:princess
 * @Date 2023/11/2 11 02 9:47
 *
 */

import com.edu.bean.User;
import com.edu.mapper.UserMapper;
import com.edu.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;

    @Override
    public Map<String, Object> login(User user) {
        Map<String,Object> map = new HashMap<>();
        User loginUser = userMapper.selectUserByNameAndPass(user.getUsername(), user.getPassword());
        if (loginUser != null){
            map.put("loginUser","登陆成功");
        }else{
            if(userMapper.selectUserByName(user.getUsername()) == null){
                map.put("msg","用户名不存在");
            }else{
                map.put("msg","密码错误");
            }
        }
        return map;
    }

    @Override
    public int register(User user) {
        if (userMapper.selectUserByName(user.getUsername()) != null){
            return -1;
        }
        return userMapper.insertUser( user);
    }

    @Override
    public Map<String, Object> updatePass(String username, String oldPass, String newPass) {
        Map<String,Object> map = new HashMap<>();
        int updateUser = userMapper.updateUser(username, oldPass, newPass);
        if (userMapper.selectUserByName(username) == null){
            map.put("msg","用户名不存在");
        }else{
            map.put("msg","原始密码错误");
        }
        if (updateUser == 1){
            map.put("msg","修改成功");
        }
        return map;
    }
}
